home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Papers / Garrison / Code / SchmoozingExamples / Listing6.m < prev    next >
Encoding:
Text File  |  2001-06-23  |  2.0 KB  |  61 lines

  1. //
  2. //  Listing6.m
  3. //  SchmoozingExamples
  4. //
  5. //  Created by garrison on Fri Apr 20 2001.
  6. //  Copyright (c) 2001 Standard Orbit Software, LLC. All rights reserved.
  7. //
  8. //  Permission is granted to use this code for any purpose, at your own risk.
  9. //  No warranties are expressed or implied.
  10.  
  11. #import <Foundation/Foundation.h>
  12. #import <OmniNetworking/OmniNetworking.h>
  13.  
  14. // A Threaded Concurrent TCP Server Using OmniNetworking
  15.  
  16. #import "Connection.h"
  17.  
  18. int main( int argc, char** argv ) {
  19.     ONTCPSocket *serverSocket;
  20.     unsigned short serverPort = 1701;
  21.     NSAutoreleasePool *mainPool;
  22.     
  23.     mainPool = [[NSAutoreleasePool alloc] init];
  24.     
  25.     serverSocket = [ONTCPSocket tcpSocket];
  26.     // Master Step 1. Allocate a socket
  27.     
  28.     [serverSocket startListeningOnLocalPort: serverPort];
  29.     // Master Step 2. Establish a listener
  30.     
  31.     
  32.     while (1) {
  33.             NSAutoreleasePool *loopPool = nil;
  34.             Connection *client = nil;
  35.             ONTCPSocket *connectionSocket = nil;
  36.                 
  37.             loopPool = [[NSAutoreleasePool alloc] init];
  38.     
  39.             NSLog(@"Main thread listening for next connection");
  40.             connectionSocket = [serverSocket acceptConnectionOnNewSocket];
  41.             // Master Step 3. Accept new connections
  42.     
  43.             client = [[Connection alloc] initWithConnectedSocket:connectionSocket];
  44.             [client autorelease];
  45.             // Slave Step 1. Receive the connection’s socket in our connection
  46.             // handling object.
  47.     
  48.             NSLog(@"Detaching thread to handle the connection");
  49.             [NSThread detachNewThreadSelector:@selector(processConnection)
  50.                         toTarget:client withObject:nil];
  51.             // Slave Step 2. Interact with the client, in a separate thread,
  52.             // by way of Connection’s processConnection: method.
  53.             // Slave Step 3. Closing the connection, is handled in the spawned thread.
  54.     
  55.             [loopPool release];
  56.     }
  57.     
  58.     [mainPool release];    
  59.     return(0);
  60.     exit(0);
  61. }